"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 导入导出、备份恢复工具支持M*兼容性
Case Name   : 创建对象后，使用comment on 语句添加注释，导出后再导入
Create At   : 2023.05.04
Owner       : z@wan005
Description :
    1.创建兼容b库
    2.兼容b库下建表并创建索引后使用comment on语句添加注释信息
    3.兼容b库下创建存储过程并使用comment on语句添加注释信息
    4.兼容b库下创建自定义函数并使用comment on语句添加注释信息
    5.使用gs_dump工具导出表为tar格式
    6.导入之前导出的数据到新库
    7.连接导入库查询表
    8.连接导入库查询索引
    9.连接导入库查询存储过程
    10.连接导入库查询自定义函数
    11.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.成功
    6.成功
    7.成功，注释导入成功
    8.成功，注释导入成功
    9.成功，注释导入成功
    10.成功，注释导入成功
    11.成功
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class GsDump0042(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.dbuser_node = Node('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = "db_dump_0042"
        self.db_name_01 = "db_dump_0042_01"
        self.tb_name = "tb_dump_0042"
        self.index_name = "index_dump_0042"
        self.pro_name = "pro_dump_0042"
        self.func_name = "func_dump_0042"
        self.dump_path = os.path.join(macro.DB_BACKUP_PATH, 'dump0042')

    def test_server_tools(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        text = '----step1: 创建兼容b库 expect: 成功----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility 'b';" \
                  f"drop database if exists {self.db_name_01};" \
                  f"create database {self.db_name_01} dbcompatibility 'b';"
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_result)
        self.assertEqual(2, sql_result.count(
            self.constant.CREATE_DATABASE_SUCCESS), '执行失败:' + text)

        text = '----step2: 兼容b库下建表并创建索引后使用comment on语句添加注释信息 ' \
               'expect: 成功----'
        self.log.info(text)
        sql_cmd = f'''drop table if exists {self.tb_name};
        create table {self.tb_name}(id int);
        comment on table {self.tb_name} is '表的注释';
        comment on column {self.tb_name}.id is '序号';
        drop index if exists {self.index_name};
        create index {self.index_name} on {self.tb_name} using btree(id);
        comment on index {self.index_name} is 'btree索引';
        drop procedure if exists {self.pro_name};
        create procedure {self.pro_name}(int,int)as begin \
        select 1+2;end;'''
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd,
                                                   dbname=self.db_name)
        self.log.info(sql_result)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, sql_result,
                      '执行失败:' + text)
        self.assertIn(self.constant.CREATE_INDEX_SUCCESS_MSG, sql_result,
                      '执行失败:' + text)
        self.assertEqual(3, sql_result.count('COMMENT'), '执行失败:' + text)

        text = '----step3: 使用comment on语句给存储过程添加注释信息 expect: 成功----'
        self.log.info(text)
        sql_cmd = f'''comment on function {self.pro_name}(int,int) is \
        'comment_存储过程';'''
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd,
                                                   dbname=self.db_name)
        self.log.info(sql_result)
        self.assertIn('COMMENT', sql_result, '执行失败:' + text)

        text = '----step4: 兼容b库下创建自定义函数并使用comment on语句添加注释信息 ' \
               'expect: 成功----'
        self.log.info(text)
        sql_cmd = f'''drop function if exists {self.func_name};
        create function {self.func_name}(n int)  returns varchar(50) \
        return (select n+1);
        comment on function {self.func_name}(int) is 'comment_自定义函数';'''
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd,
                                                   dbname=self.db_name)
        self.log.info(sql_result)
        self.assertIn(self.constant.CREATE_FUNCTION_SUCCESS_MSG, sql_result,
                      '执行失败:' + text)
        self.assertIn('COMMENT', sql_result, '执行失败:' + text)

        text = '----step5: 使用gs_dump工具导出表为tar格式 expect: 成功----'
        self.log.info(text)
        gs_dump_cmd = f"source {macro.DB_ENV_PATH};" \
                      f"gs_dump " \
                      f"-p {self.dbuser_node.db_port} " \
                      f"{self.db_name} -F t " \
                      f"-f {self.dump_path}"
        self.log.info(gs_dump_cmd)
        dump_msg = self.dbuser_node.sh(gs_dump_cmd).result()
        self.log.info(dump_msg)
        self.assertIn(f"dump database {self.db_name} successfully",
                      dump_msg, '执行失败:' + text)

        text = '----step6: 导入之前导出的数据到新库 expect: 成功----'
        self.log.info(text)
        gs_restore_cmd = f"source {macro.DB_ENV_PATH};" \
                         f"gs_restore -p {self.dbuser_node.db_port} " \
                         f"-d {self.db_name_01} " \
                         f"{self.dump_path}"
        self.log.info(gs_restore_cmd)
        restore_msg = self.dbuser_node.sh(gs_restore_cmd).result()
        self.log.info(restore_msg)
        self.assertIn(self.constant.RESTORE_SUCCESS_MSG, restore_msg,
                      '执行失败:' + text)

        text = '----step7: 连接导入库查询表 expect: 成功，注释导入成功----'
        self.log.info(text)
        sql_cmd = f"\d+ {self.tb_name}"
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd,
                                                   dbname=self.db_name_01)
        self.log.info(sql_result)
        self.assertIn(
            ' id     | integer |           | plain   |              | 序号',
            sql_result, '执行失败:' + text)

        text = '----step8: 连接导入库查询索引 expect: 成功，注释导入成功----'
        self.log.info(text)
        sql_cmd = f"\di+ {self.index_name}"
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd,
                                                   dbname=self.db_name_01)
        self.log.info(sql_result)
        self.assertIn('btree索引', sql_result, '执行失败:' + text)

        text = '----step9: 连接导入库查询存储过程 expect: 成功，注释导入成功----'
        self.log.info(text)
        sql_cmd = f"\df+ {self.pro_name}"
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd,
                                                   dbname=self.db_name_01)
        self.log.info(sql_result)
        self.assertIn('comment_存储过程', sql_result, '执行失败:' + text)

        text = '----step10: 连接导入库查询自定义函数 expect: 成功，注释导入成功----'
        self.log.info(text)
        sql_cmd = f"\df+ {self.func_name}"
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd,
                                                   dbname=self.db_name_01)
        self.log.info(sql_result)
        self.assertIn('comment_自定义函数', sql_result, '执行失败:' + text)

    def tearDown(self):
        text = '---step11: 清理环境 expect: 成功----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"drop database if exists {self.db_name_01};"
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd,
                                                   dbname='postgres')
        self.log.info(sql_result)
        rm_cmd = f"rm -rf {self.dump_path}"
        self.log.info(rm_cmd)
        rm_msg = self.dbuser_node.sh(rm_cmd).result()
        self.log.info(rm_msg)
        self.assertEqual(2,
                         sql_result.count(self.constant.DROP_DATABASE_SUCCESS),
                         '执行失败:' + text)
        self.assertEqual('', rm_msg, '执行失败:' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
